﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;

namespace PlanetViewer
{
    class BufferedVerts
    {
        private VertexBuffer _bufferVerts;
        private IndexBuffer _vertIndex;
        private GraphicsDevice graphicsDevice;

        public BufferedVerts(GraphicsDevice graphicsdevice)
        {
            this.graphicsDevice = graphicsdevice; //Maintains a local reference to the display adapter.
        }

        public void UpdateBuffer()
        {
            //CopyToBuffers(Planet.Terrain.UGVertices, Planet.Terrain.UGIndices);  //Probably shouldn't be using a static reference for "Planet"
        }

        private void CopyToBuffers(VertexPositionColor[] verts, int[] indices)
        {
            _bufferVerts = new VertexBuffer(graphicsDevice, VertexPositionColor.VertexDeclaration, verts.Length, BufferUsage.WriteOnly);
            _vertIndex = new IndexBuffer(graphicsDevice, typeof(int), indices.Length, BufferUsage.WriteOnly);
            _bufferVerts.SetData(verts);
            _vertIndex.SetData(indices);
        }

        public void DrawFromBuffer()
        {
            graphicsDevice.Indices = _vertIndex;
            graphicsDevice.SetVertexBuffer(_bufferVerts);
            graphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, _vertIndex.IndexCount, 0, _vertIndex.IndexCount / 3);                
        }
    }

}
